﻿#include "PlayerLevelUpCacheData.h"

PlayerLevelUpCacheData::PlayerLevelUpCacheData()
	: m_maxCount(0)
{
}

PlayerLevelUpCacheData::~PlayerLevelUpCacheData()
{
	Clear();
}

bool PlayerLevelUpCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void PlayerLevelUpCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool PlayerLevelUpCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/player/playerLevelUp.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/player/playerLevelUp.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();
	
	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void PlayerLevelUpCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		PlayerLevelUp* p = new PlayerLevelUp;
		_safe_check(p);

		p->mId = jm["id"].asUInt();
		p->mExp = jm["exp"].asUInt();
		p->mHeroLvLimit = jm["hero_lv_limit"].asUInt();
		p->mPowerReward = jm["power_reward"].asUInt();
		p->mMaxPower = jm["max_power"].asUInt();
		p->mMaxFriend = jm["max_friend"].asUInt();
		p->mMaxVitality = jm["max_vitality"].asUInt();
		p->mResurrectionCD = jm["resurrection_cd"].asUInt();
		p->mdpslimit = jm["dps_double_hero_limit"].asUInt();

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}


PlayerLevelUp* PlayerLevelUpCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool PlayerLevelUpCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	PlayerLevelUp* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error12");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["exp"] = Json::Value(p->mExp);
	_jm["hero_lv_limit"] = Json::Value(p->mHeroLvLimit);
	_jm["power_reward"] = Json::Value(p->mPowerReward);
	_jm["max_power"] = Json::Value(p->mMaxPower);
	_jm["max_friend"] = Json::Value(p->mMaxFriend);
	_jm["max_vitality"] = Json::Value(p->mMaxVitality);

	_safe_guard_end;

	return true;
}

uint32_t PlayerLevelUpCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}

